﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProdeManagmente.Model;
using ProdeManagmente.DAO;
using ProdeManagmente.Libs;

namespace ProdeManagmente.Forms.ABMS
{
    public partial class FrmMatch : Form
    {
        private Match match;
        private MatchService matchService;
        private TournamenteService tournametService;
        private bool changed = false;

        public FrmMatch(Match match)
        {
            InitializeComponent();
            this.match = match;
            loadTournaments();
            loadTeams(0);
            loadData();
            changedValue(false);
        }

        private void loadData()
        {
            if (match == null)
                return;

            txtId.Text = match.getId().ToString();
            cmbTournament.SelectedValue = match.getTournament().getId();
            cmbTeam1.SelectedValue = match.getTeam1().getId();
            cmbTeam2.SelectedValue = match.getTeam2().getId();
            dtpDate.Value = match.getDate();
            dtpTime.Value = match.getTime();
        }

        private void loadTournaments()
        { 
            tournametService = TournamenteService.getTournamenteServiceInstance();

            String SQL = "Finish > '" + DateTime.Today.ToString("o") + "' ";
            List<Tournament> tournamentList = tournametService.getTournamentListFilter(SQL);
            cmbTournament.DisplayMember = "name";
            cmbTournament.ValueMember = "id";
            cmbTournament.DataSource = tournamentList;
        }

        private void loadTeams(long idTournament)
        {
            tournametService = TournamenteService.getTournamenteServiceInstance();
            List<Team> teamList1 = tournametService.getTeamTournament(idTournament);
            List<Team> teamList2 = tournametService.getTeamTournament(idTournament);
            cmbTeam1.DisplayMember = "name";
            cmbTeam1.ValueMember = "id";
            cmbTeam1.DataSource = teamList1;

            cmbTeam2.DisplayMember = "name";
            cmbTeam2.ValueMember = "id";
            cmbTeam2.DataSource = teamList2;
        }

        private void blindData()
        {
            if (match == null)
                match = new Match();

            match.setTournament((Tournament)cmbTournament.SelectedItem);
            match.setTeam1((Team)cmbTeam1.SelectedItem);
            match.setTeam2((Team)cmbTeam2.SelectedItem);
            match.setDate(dtpDate.Value);
            match.setTime(dtpTime.Value);
        }

        private void changeTitle()
        {
            String title = "Equipo";
            if (changed)
            {
                title = title + "*";
            }
            this.Text = title;
        }

        private void changedValue(bool value)
        {
            changed = value;
            changeTitle();
        }

        private void save()
        {
            matchService = MatchService.getMatchServiceInstance();

            try
            {
                blindData();
                this.match = matchService.save(this.match);
                changedValue(false);
                loadData();
                lblInfo.Text = "Operacion exitosa";
            }
            catch (BusinessException error)
            {
                new MensajeSalida(error).showMensaje();
            }
        }

        private bool exit()
        {
            if (changed)
            {
                DialogResult result = MessageBox.Show("Desea grabar los cambios?", "Salir", MessageBoxButtons.YesNoCancel);

                if (result == DialogResult.Yes)
                {
                    save();
                    return false;
                }
                else if (result == DialogResult.No)
                {
                    changedValue(false);
                    return false;
                }
                else if (result == DialogResult.Cancel)
                {
                    return true;
                }
            }
            return false;
        }

        private void cmbTournament_SelectedIndexChanged(object sender, EventArgs e)
        {
            Tournament tournamente = (Tournament)cmbTournament.SelectedItem;
            if(tournamente != null)
                loadTeams(tournamente.getId());
            changedValue(true);
        }

        private void cmbTeam1_SelectedIndexChanged(object sender, EventArgs e)
        {
            changedValue(true);
        }

        private void cmbTeam2_SelectedIndexChanged(object sender, EventArgs e)
        {
            changedValue(true);
        }

        private void dtpDate_ValueChanged(object sender, EventArgs e)
        {
            changedValue(true);
        }

        private void dtpTime_ValueChanged(object sender, EventArgs e)
        {
            changedValue(true);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            save();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (!exit())
                this.Close();
        }

        private void FrmMatch_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = exit();
        }
    }
}
